package se.peterbjorkman.android.trafikkamera;

import se.peterbjorkman.android.trafikkamera.providers.CameraContentProvider;
import se.peterbjorkman.android.trafikkamera.providers.CameraDefs;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ViewGroup.LayoutParams;
import android.widget.CursorTreeAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

public abstract class CameraTreeActivity extends ExpandableListActivity {

    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter2 {

        public MyExpandableListAdapter(Context context, int groupLayout,
                int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                int[] childrenTo,Cursor cur) {
            super(context, cur, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
                    childrenTo);
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            // Given the group, we return a cursor for all the children within that group
    		String[] projection = new String[] { 
    				CameraDefs.ID, 
    				CameraDefs.NAME,
    				CameraDefs.ROAD,
    				CameraDefs.CAMERA_URL };
    		
    		String childFilter = getChildFilter(groupCursor);
    		
    		return managedQuery(
    				CameraContentProvider.CONTENT_URI,
    				projection,childFilter , null, getChildOrder());

        }
    }

    private CursorTreeAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set up our adapter
        mAdapter = new MyExpandableListAdapter(
                this,
                getRootLayout(),
                android.R.layout.simple_expandable_list_item_2,
                getRootNames(),
                getRootViesIds(),
                new String[] { CameraDefs.NAME, CameraDefs.ROAD},
                new int[] { android.R.id.text1 , android.R.id.text2 },
                getRootCursor());

        setListAdapter(mAdapter);
        
        registerForContextMenu(getExpandableListView());
        View emptyView = getEmptyView();
        if (emptyView != null){
        	ExpandableListView listView = getExpandableListView();
        	emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        	emptyView.setVisibility(View.GONE);
        	((ViewGroup)listView.getParent()).addView(emptyView);
        	getExpandableListView().setEmptyView(emptyView);
        }
    }

    protected View getEmptyView(){
    	return null;
    }
    
    protected abstract String[] getRootNames();

	protected abstract int[] getRootViesIds();

	protected abstract int getRootLayout();

	protected abstract Cursor getRootCursor();
	
	protected abstract String getChildFilter(Cursor rootCursor);

	protected abstract String getChildOrder();

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Null out the group cursor. This will cause the group cursor and all of the child cursors
        // to be closed.
        mAdapter.changeCursor(null);
        mAdapter = null;
    }

    @Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		Intent i = new Intent(this, CameraPictureActivity.class);
		i.putExtra(CameraPictureActivity.EXTRA_ID, id);
		startActivity(i);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		ExpandableListContextMenuInfo contextMenuInfo = (ExpandableListContextMenuInfo)menuInfo;
		long packedPosition = contextMenuInfo.packedPosition;
		int positionType = ExpandableListView.getPackedPositionType(packedPosition);
		switch (positionType){
			case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
				//int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
				long groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
				long groupId = mAdapter.getGroupId((int)groupPosition);
				onCreateContextMenuChild(menu, groupId, contextMenuInfo.id);
			break;
			case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
				onCreateContextMenuGroup(menu, contextMenuInfo.id);
			break;
		}
	}
	
	protected void onCreateContextMenuChild(ContextMenu menu, long groupId, long id){
		
	}
	
	protected void onCreateContextMenuGroup(ContextMenu menu, long id){
		
	}
	

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
		ExpandableListContextMenuInfo contextMenuInfo = (ExpandableListContextMenuInfo)item.getMenuInfo();
		long packedPosition = contextMenuInfo.packedPosition;
		int positionType = ExpandableListView.getPackedPositionType(packedPosition);
		long groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
		switch (positionType){
			case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
				//int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
				long groupId = mAdapter.getGroupId((int)groupPosition);
				boolean ret = onContextItemSelectedChild(item.getItemId(), groupId, contextMenuInfo.id);
				if (ret)
					return true;
			break;
			case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
				ret = onContextItemSelectedGroup(item.getItemId(), contextMenuInfo.id, (int)groupPosition);
				if (ret)
					return true;
			break;
		}
    	return super.onContextItemSelected(item);
    }

	protected boolean onContextItemSelectedChild(int itemId, long groupId, long childId) {
		return false;
	}

	protected boolean onContextItemSelectedGroup(int itemId, long id, int groupPosition) {
		return false;
	}
	
	protected Cursor getGroupCursor(int position){
		return mAdapter.getGroup(position);
	}
}